// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::sha256;
use hash;
use memio;

// Tests siging and verifying of a message against a signature that was
// generated using golang 1.22.
@test fn pss_sig() void = {
	let salt: [_]u8 = [
		0x3e, 0xe9, 0x4c, 0x10, 0x61, 0x1d, 0x71, 0xe4, 0x8a, 0x49,
		0xb3, 0xd1, 0x56, 0xb5, 0x32, 0x52, 0x95, 0x1a, 0xad, 0xa6,
		0x40, 0xe2, 0xb7, 0xed, 0x05, 0x23, 0x87, 0xa7, 0xaf, 0x3f,
		0x9d, 0x1c, 0x70, 0x9d, 0x74, 0x75, 0x9c, 0x70, 0x29, 0x3a,
		0x55, 0x18, 0xf5, 0x35, 0xeb, 0x9a, 0x83, 0x39, 0xe0, 0x67,
		0x4f, 0xd8, 0x84, 0x5b, 0xcf, 0x02, 0xcf, 0xc0, 0x70, 0xcb,
		0xa1, 0x45, 0x0e, 0xd9, 0x76, 0xa9, 0x48, 0x8a, 0x06, 0x85,
		0x67, 0x67, 0x62, 0x3c, 0x31, 0x59, 0x81, 0x10, 0x06, 0x43,
		0x38, 0xd2, 0x2f, 0xe2, 0xfa, 0xc1, 0xc1, 0x8e, 0x3a, 0x0e,
		0x2c, 0x70, 0xec, 0x85, 0x28, 0xec, 0x43, 0xa1, 0x7b, 0xe2,
		0x75, 0xac, 0x0a, 0xe6, 0x59, 0x7d, 0x48, 0x04, 0x9d, 0x2b,
		0x4f, 0xb6, 0x32, 0x5a, 0x50, 0xda, 0x8b, 0xce, 0x93, 0xe7,
		0xed, 0xac, 0x51, 0x30, 0xb9, 0x01, 0xb3, 0x2e, 0x88, 0x34,
		0xc9, 0x00, 0xf8, 0x3c, 0xff, 0x0d, 0x9c, 0xea, 0x43, 0x7e,
		0xcc, 0xbc, 0x64, 0xda, 0x72, 0x16, 0x19, 0x2d, 0xe2, 0xd5,
		0xe9, 0x29, 0xfe, 0x17, 0x6a, 0xbd, 0xdc, 0x10, 0x7b, 0xbf,
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
		0x77, 0x6e, 0x6f, 0xed, 0x44, 0xc5, 0xf1, 0x6a, 0x19, 0x5a,
	];

	let sig: [_]u8 = [
		0x6d, 0x24, 0x30, 0xc3, 0x77, 0x76, 0x09, 0x3b, 0xac, 0xd7,
		0x30, 0x88, 0xab, 0x72, 0xb5, 0xd2, 0xee, 0xab, 0x3d, 0x26,
		0xa3, 0x8b, 0xa5, 0x74, 0xf6, 0x6b, 0x7b, 0x57, 0xcd, 0xe8,
		0xe3, 0xe7, 0x17, 0xbe, 0xf9, 0x15, 0x18, 0x38, 0x34, 0x51,
		0x3f, 0x0e, 0x8b, 0x80, 0x0b, 0xeb, 0xf2, 0xf8, 0x18, 0x38,
		0x0c, 0x6b, 0xfe, 0x30, 0x31, 0x62, 0x17, 0xb5, 0xef, 0x81,
		0xfa, 0x3b, 0x34, 0x03, 0xa7, 0x9b, 0xd8, 0x7b, 0xd2, 0xf4,
		0xb6, 0x93, 0x4e, 0xeb, 0x36, 0x52, 0xa0, 0x07, 0x93, 0xec,
		0x26, 0x01, 0xde, 0x32, 0xc9, 0xa1, 0x7a, 0x37, 0x6f, 0x57,
		0x57, 0x69, 0x12, 0x75, 0xe3, 0x37, 0x97, 0x4f, 0x9a, 0x52,
		0xe9, 0xb9, 0xb9, 0x80, 0xcb, 0x21, 0xdb, 0x6b, 0x48, 0xf2,
		0xc2, 0xdf, 0x3c, 0x29, 0xce, 0x6a, 0xab, 0x69, 0x02, 0xe3,
		0x78, 0xb1, 0xe5, 0x50, 0xea, 0x35, 0x5e, 0xf7, 0x8e, 0x9e,
		0x9e, 0x41, 0x2e, 0xb0, 0x46, 0x29, 0x54, 0xa3, 0x96, 0x4f,
		0x20, 0x44, 0x42, 0x65, 0x5b, 0x47, 0x85, 0xd6, 0x1d, 0x36,
		0xd6, 0xc2, 0xdf, 0x4f, 0xec, 0x7b, 0x72, 0xde, 0x88, 0x67,
		0x27, 0x2e, 0x1d, 0xa1, 0x7d, 0x8e, 0x36, 0xef, 0x0f, 0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
		0x94, 0xf1, 0xd1, 0x26, 0x17, 0x4e, 0xc8, 0x29, 0xbb, 0xb2,
		0x6a, 0xb8, 0x72, 0x0e, 0xb2, 0xf4, 0x49, 0x12, 0x47, 0x16,
		0x1a, 0xfd, 0xae, 0x7e, 0x16, 0x68, 0x0b, 0x97, 0x71, 0x52,
		0x49, 0x92, 0x62, 0x26,
	];

	let digest: [sha256::SZ]u8 = [0...];
	let hf = sha256::sha256();
	hash::write(&hf, msg);
	hash::sum(&hf, digest);

	let pk = sign3072.priv;
	let priv: [PRIVKEYSZ]u8 = [0...];
	privkey_init(priv, sign3072.priv)!;

	let randrd = memio::fixed(salt);
	let newsig: [384]u8 = [0...];

	let signbuf: [PSS_SIGNBUFSZ]u8 = [0...];
	pss_sign(priv, digest, newsig, &hf, &randrd, signbuf)!;

	assert(bytes::equal(newsig, sig));

	let pub: [PUBKEYSZ]u8 = [0...];
	pubkey_init(pub, sign3072.pub)!;

	let verifybuf: [PSS_VERIFYBUFSZ]u8 = [0...];
	match (pss_verify(pub, digest, sig, &hf, verifybuf)) {
	case void => void;
	case let e: error =>
		abort(strerror(e));
	};

	sig[10] += 1;
	match (pss_verify(pub, digest, sig, &hf, verifybuf)) {
	case void =>
		abort("should fail");
	case let e: error => void;
	};

	sig[10] -= 1;
	digest[10] += 1;
	match (pss_verify(pub, digest, sig, &hf, verifybuf)) {
	case void =>
		abort("should fail");
	case let e: error => void;
	};
};
